﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.WVB.Framework;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.UI
{
    [TemplatePart(Name = ExplorerActionsBase.AlphaSortingElementName, Type = typeof(UIElement))]
    [TemplatePart(Name = ExplorerActionsBase.DateSortingElementName, Type = typeof(UIElement))]
    [TemplatePart(Name = ExplorerActionsBase.PagingElementName, Type = typeof(UIElement))]
    [TemplatePart(Name = ExplorerActionsBase.RankSortingElementName, Type = typeof(UIElement))]
    public class ExplorerActionsBase : Control
    {
        private const string AlphaSortingElementName = "AlphaSortingElement";
        private const string DateSortingElementName = "DateSortingElement";
        private const string PagingElementName = "PagingElement";
        private const string RankSortingElementName = "RankSortingElement";


        protected UIElement AlphaSorting { get; set; }
        protected UIElement DateSorting { get; set; }
        protected UIElement Paging { get; set; }
        protected UIElement Ranking { get; set; }

        public IExplorerModel ViewModel { get; private set; }

        public ExplorerActionsBase()
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                ViewModel = IoC.GetCreate<IExplorerModel>();
                DataContext = ViewModel;
            }
        }

        public override void OnApplyTemplate()
        {
            MouseButtonEventHandler alphaSortingHandler = null;
            MouseButtonEventHandler dateSortingHandler = null;
            MouseButtonEventHandler pagingHandler = null;
            MouseButtonEventHandler rankingHandler = null;

            base.OnApplyTemplate();

            AlphaSorting = base.GetTemplateChild(ExplorerActionsBase.AlphaSortingElementName) as UIElement;
            DateSorting = base.GetTemplateChild(ExplorerActionsBase.DateSortingElementName) as UIElement;
            Paging = base.GetTemplateChild(ExplorerActionsBase.PagingElementName) as UIElement;
            Ranking = base.GetTemplateChild(ExplorerActionsBase.RankSortingElementName) as UIElement;

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                if (AlphaSorting != null)
                {
                    if (alphaSortingHandler == null)
                    {
                        alphaSortingHandler = delegate(object sender, MouseButtonEventArgs e)
                        {
                            ViewModel.Sort(Microsoft.WVB.Framework.ExplorerSorting.Alphabetic);
                        };
                    }
                    AlphaSorting.MouseLeftButtonUp += alphaSortingHandler;
                }

                if (DateSorting != null)
                {
                    if (dateSortingHandler == null)
                    {
                        dateSortingHandler = delegate(object sender, MouseButtonEventArgs e)
                        {
                            ViewModel.Sort(Microsoft.WVB.Framework.ExplorerSorting.Date);
                        };
                    }
                    DateSorting.MouseLeftButtonUp += dateSortingHandler;
                }

                if (Paging != null)
                {
                    if (pagingHandler == null)
                    {
                        pagingHandler = delegate(object sender, MouseButtonEventArgs e)
                        {
                            ViewModel.PageNext();
                        };
                    }
                    Paging.MouseLeftButtonUp += pagingHandler;
                }

                if (Ranking != null)
                {
                    if (rankingHandler == null)
                    {
                        rankingHandler = delegate(object sender, MouseButtonEventArgs e)
                        {
                            ViewModel.Sort(Microsoft.WVB.Framework.ExplorerSorting.Rank);
                        };
                    }
                    Ranking.MouseLeftButtonUp += rankingHandler;
                }
            }
        }
    }
}
